home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9730 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  71 lines

  1. Newsgroups: comp.lang.c++
  2. Path: presby.edu!jtbell
  3. From: jtbell@presby.edu (Jon Bell)
  4. Subject: Re: What is referencing good for?
  5. Message-ID: <Dnq47s.CBv@presby.edu>
  6. Date: Mon, 4 Mar 1996 03:26:16 GMT
  7. References: <4h6ghr$70a@chaos.kulnet.kuleuven.ac.be>
  8. Organization: Presbyterian College, Clinton, South Carolina USA
  9.  
  10.  Andreas De Troy  <Andreas.DeTroy@ped.kuleuven.ac.be> wrote:
  11. >I think the ONLY reason why this referencing-thing is introduced in C++, 
  12. >is to allow operator overloading.
  13.  
  14. You're basically correct here, although "only" is a bit too strong.  In 
  15. his book "The Design and Evolution of C++", Bjarne Stroustrup writes:
  16.  
  17. >References were introduced primarily to support operator overloading. 
  18. >[...]
  19. >
  20. >C passes every function argument by value, and where passing an object 
  21. >by value would be inefficient or inappropriate the user can pass a 
  22. >pointer.  This strategy doesn't work where operator overloading is used.  
  23. >In that case, notational convenience is essential because users cannot be 
  24. >expected to insert address-of operators if the objects are large.  For 
  25. >example:
  26. >
  27. >    a = b - c;
  28. >
  29. >is acceptable (that is, conventional) notation, but
  30. >
  31. >    a = &b - &c;
  32. >
  33. >is not.  Anyway, &b-&c already has a meaning in C, and I didn't want to 
  34. >change that.
  35.  
  36. You write:
  37.  
  38. >For the rest referencing is a rather dangerous thing and I would never 
  39. >recommend using it. People who say that it allows "cleaner" code are just 
  40. >missing the point. In "C" if you see something like:
  41. >
  42. >   int a, b, c, d;
  43. >   
  44. >   swap (&a, &b);
  45. >   perform_thing (c, d);
  46. >
  47. >.. you know that a and b can be changed inside the function, and c and d 
  48. >not. This is very helpful for debugging. In C++ you don't know this, and 
  49. >you actually have to look at the exact function-definition, somewhere in 
  50. >a header, to make sure that c and d can be modified or not.
  51. >
  52. >So I would NEVER use the &-operator to produce so-called "cleaner" code.
  53.  
  54. This is a matter of opinion.  Having programmed in Fortran and Pascal for
  55. years already, I found that my most common errors in writing C programs
  56. were (a) forgetting to insert the '&' after an argument when calling a
  57. function which passes a pointer, and (b) forgetting to insert the '*' when
  58. using a pointer argument inside a function, when I wanted the thing the 
  59. pointer pointed to.  I am very grateful that C++ allows me to avoid 
  60. stupid little mistakes like that by using references instead.  (I should 
  61. note that I very seldom need to do pointer arithmetic.)
  62.  
  63. But I don't begrudge you your opinion.  C++ has the flexibility to allow 
  64. us both to follow our preferences.  I just hope I never have to modify 
  65. any of your code, because I will surely forget those blasted '*'s again!  
  66. :-) :-)
  67.  
  68. -- 
  69. Jon Bell <jtbell@presby.edu>                        Presbyterian College
  70. Dept. of Physics and Computer Science        Clinton, South Carolina USA
  71.